package dongshi.daddy.huohuo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamDemo {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(4, 2, 3, 5, 1, 2, 2, 7, 6);
        // 中间操作fillter
        System.out.println("fillter: " + list.stream().parallel().filter((v) -> v > 5).collect(Collectors.toList()));
        // distinct去重
        System.out.println("distinct: " + list.stream().parallel().distinct().collect(Collectors.toList()));
        // limit 限制元素个数
        System.out.println("limit: " + list.stream().limit(3).collect(Collectors.toList()));
        // skip 跳过前n个元素
        System.out.println("skip: " + list.parallelStream().skip(7).collect(Collectors.toList()));
        // map 拼加A转成字符串
        System.out.println("map: " + list.parallelStream().map((v) -> v + "A").collect(Collectors.toList()));
        // mapToDouble 转Double
        System.out.println("mapToDouble: " + list.parallelStream().mapToDouble((v) -> v).boxed().collect(Collectors.toList()));
        // mapToInt 转Integer
        System.out.println("mapToInt: " + list.parallelStream().mapToInt((v) -> v).boxed().collect(Collectors.toList()));

        // flatMap
        List<List<String>> list111 = new ArrayList<>();
        List<String> listA = new ArrayList<>();
        listA.add("A1");
        listA.add("A2");
        List<String> listB = new ArrayList<>();
        listB.add("B1");
        listB.add("B2");
        list111.add(listA);
        list111.add(listB);
        System.out.println("flatMap: " + list111.stream().flatMap(v -> v.stream()).collect(Collectors.toList()));

        // sorted
        // List<Integer> list = Arrays.asList(4, 2, 3, 5, 1, 2, 2, 7, 6);
        System.out.println("sorted: " + list.stream().parallel().sorted().collect(Collectors.toList()));
        // sorted(Comparator)
        System.out.println("sorted(Comparator): " + list.stream().sorted((o1, o2) -> o2 - o1).collect(Collectors.toList()));

        // 终止操作 anyMatch
        System.out.println("anyMatch: " + list.stream().anyMatch(v -> v > 6));
        // 终止操作 allMatch
        System.out.println("allMatch: " + list.stream().allMatch(v -> v < 0));

        // reduce 求和 初始值给0
        // 计算过程 0+list[0] = result1 result1+list[1]=result2 ...
        System.out.println("reduce 求和 初始值给0 : " + list.stream().reduce(0, (v1, v2) -> v1 + v2));

        // reduce 求积 初始值给1
        // 计算过程 1*list[0] = result1 result1*list[1]=result2 ...
        System.out.println("reduce 求积 初始值给1 : " + list.stream().reduce(1, (v1, v2) -> v1 * v2));

        // findFirst
        System.out.println("findFirst: " + list.stream().findFirst().get());
        // findAny
        System.out.println("findAny: " + list.stream().findAny().get());
        // max
        System.out.println("max: " + list.stream().max((v1, v2) -> v1 - v2).get());

        // count 大于4的个数
        System.out.println("count 大于4的个数: " + list.stream().filter(v -> v > 4).count());
    }
}
